﻿
using System;
using System.Text;
using WeShare.WeiXin.Common;

namespace WeShare.WeiXin.Mp.MsgEntity
{
    public class BaseMsg
    {
        /// <summary>
        /// 开发者微信号
        /// </summary>
        public string ToUserName { get; set; }
        /// <summary>
        /// 发送方账号（一个openid）
        /// </summary>
        public string FromUserName { get; set; }
        /// <summary>
        /// 消息创建时间 （整型）
        /// </summary>
        public string CreateTime { get; set; }
        /// <summary>
        /// 消息类型
        /// </summary>
        public string MsgType { get; set; }
        /// <summary>
        /// 普通消息专用。 事件消息中，此字段为空
        /// </summary>
        public string MsgId { get; set; }
        /// <summary>
        /// 微信接入时的参数
        /// </summary>
        public EnterParamConfig Param { get; set; }
        /// <summary>
        /// 上一次消息的时间。非微信推送字段，为本sdk记录信息。
        /// </summary>
        public DateTime LastTime { get; set; }
        #region 响应请求
        /// <summary>
        /// 回复文本消息
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <param name="param">接入url时的参数</param>
        public string ResponseTxt(string content)
        {
            var data = new StringBuilder();
            data.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", FromUserName);
            data.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", ToUserName);
            data.AppendFormat("<CreateTime>{0}</CreateTime>", Utils.GetTimeStamp());
            data.AppendFormat("<MsgType><![CDATA[text]]></MsgType>");
            data.AppendFormat("<Content><![CDATA[{0}]]></Content></xml>", content);
            return Response(data.ToString());
        }
        /// <summary>
        /// 回复图片消息
        /// </summary>
        /// <param name="mediaId">图片消息的媒体ID</param>
        /// <param name="param"></param>
        public string ResponseImg(string mediaId)
        {
            var data = new StringBuilder();
            data.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", FromUserName);
            data.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", ToUserName);
            data.AppendFormat("<CreateTime>{0}</CreateTime>", Utils.GetTimeStamp());
            data.AppendFormat("<MsgType><![CDATA[image]]></MsgType>");
            data.AppendFormat("<Image><MediaId><![CDATA[{0}]]></MediaId></Image></xml>", mediaId);
            return Response(data.ToString());
        }
        /// <summary>
        /// 回复语音消息
        /// </summary>
        /// <param name="mediaId">语音消息的媒体ID</param>
        /// <param name="param"></param>
        public string ResponseVoice(string mediaId)
        {
            var data = new StringBuilder();
            data.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", FromUserName);
            data.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", ToUserName);
            data.AppendFormat("<CreateTime>{0}</CreateTime>", Utils.GetTimeStamp());
            data.AppendFormat("<MsgType><![CDATA[voice]]></MsgType>");
            data.AppendFormat("<Voice><MediaId><![CDATA[{0}]]></MediaId></Voice></xml>", mediaId);
            return Response(data.ToString());
        }
        /// <summary>
        /// 回复视频
        /// </summary>
        /// <param name="video"></param>
        /// <param name="param"></param>
        public string ResponseVideo(ResponseVideo video)
        {
            var data = new StringBuilder();
            data.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", FromUserName);
            data.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", ToUserName);
            data.AppendFormat("<CreateTime>{0}</CreateTime>", Utils.GetTimeStamp());
            data.AppendFormat("<MsgType><![CDATA[video]]></MsgType>");
            data.AppendFormat("<Video><MediaId><![CDATA[{0}]]></MediaId>", video.MediaId);
            data.AppendFormat("<Title><![CDATA[{0}]]></Title>", video.Title);
            data.AppendFormat("<Description><![CDATA[{0}]]></Description></Video></xml>", video.Description);
            return Response(data.ToString());
        }

        public string ResponseMusic(ResponseMusic music)
        {
            var data = new StringBuilder();
            data.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", FromUserName);
            data.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", ToUserName);
            data.AppendFormat("<CreateTime>{0}</CreateTime>", Utils.GetTimeStamp());
            data.AppendFormat("<MsgType><![CDATA[music]]></MsgType>");
            data.AppendFormat("<Title><![CDATA[{0}]]></Title>", music.Title);
            data.AppendFormat("<Description><![CDATA[{0}]]></Description>", music.Description);
            data.AppendFormat("<MusicUrl><![CDATA[{0}]]></MusicUrl>", music.MusicURL);
            data.AppendFormat("<HQMusicUrl><![CDATA[{0}]]></HQMusicUrl>", music.HQMusicUrl);
            data.AppendFormat("<ThumbMediaId><![CDATA[{0}]]></ThumbMediaId></Music></xml>", music.ThumbMediaId);
            return Response(data.ToString());
        }

        public string ResponseArts(ResponseMusic music)
        {
            var data = new StringBuilder();
            data.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>", FromUserName);
            data.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>", ToUserName);
            data.AppendFormat("<CreateTime>{0}</CreateTime>", Utils.GetTimeStamp());
            data.AppendFormat("<MsgType><![CDATA[music]]></MsgType>");
            data.AppendFormat("<Title><![CDATA[{0}]]></Title>", music.Title);
            data.AppendFormat("<Description><![CDATA[{0}]]></Description>", music.Description);
            data.AppendFormat("<MusicUrl><![CDATA[{0}]]></MusicUrl>", music.MusicURL);
            data.AppendFormat("<HQMusicUrl><![CDATA[{0}]]></HQMusicUrl>", music.HQMusicUrl);
            data.AppendFormat("<ThumbMediaId><![CDATA[{0}]]></ThumbMediaId></Music></xml>", music.ThumbMediaId);
            return Response(data.ToString());
        }

        /// <summary>
        /// 转发至多客服。可指定客服人员
        /// </summary>
        /// <param name="param"></param>
        /// <param name="account">客服账号</param>
        public string TransDkf(string account = "")
        {
            var resxml = new StringBuilder();
            resxml.AppendFormat("<xml><ToUserName><![CDATA[{0}]]></ToUserName>",
            FromUserName);
            resxml.AppendFormat("<FromUserName><![CDATA[{0}]]></FromUserName>",
            ToUserName);
            resxml.AppendFormat("<CreateTime>{0}</CreateTime>",
                Utils.ConvertDateTimeInt(DateTime.Now));
            resxml.Append("<MsgType><![CDATA[transfer_customer_service]]></MsgType>");
            if (account != "")
            {
                resxml.AppendFormat("<TransInfo><KfAccount><![CDATA[{0}]]></KfAccount></TransInfo>", account);
            }
            resxml.AppendFormat("</xml>");
            return Response(resxml.ToString());
        }

        private string Response(string data)
        {
            if (Param != null && Param.IsAes)
            {
                var wxcpt = new WXBizMsgCrypt(Param.token, Param.EncodingAESKey,
                Param.appid);
                wxcpt.EncryptMsg(data, Utils.GetTimeStamp().ToString(), Utils.GetTimeStamp().ToString(),
                ref data);
            }
            return data;
        }

        #endregion
    }
}
